package web.javas.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import web.javas.entity.User;
import web.javas.service.UserService;
import web.javas.service.impl.UserServiceImpl;

import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
	UserService userService=new UserServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		String action = req.getParameter("action");
		System.out.println("Servlet action: " + action);
		switch (action) {
			case "ToLogin":
				toLogin(req,resp); break;
			case "Login":
				login(req,resp); break;
			case "UserPage":
				toUserPage(req,resp); break;
			default:
				System.out.println("未匹配到合适的选项");
		}
	}

	private void toUserPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("ToUserPage");

		int id = Integer.parseInt(req.getParameter("id"));
		User user=userService.getUserById(id);

		HttpSession session=req.getSession();
		session.setAttribute("username",user.getUsername());
		session.setAttribute("email",user.getEmail());
		session.setAttribute("phone",user.getPhone());

		req.getRequestDispatcher("/user/userpage.jsp").forward(req,resp);
	}

	private void toLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		System.out.println("Servlet: toLogin");
		req.getRequestDispatcher("/user/login.jsp").forward(req,resp);
	}
	private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		System.out.println("username: " + username);
		System.out.println("password: " + password);
		User user = userService.login(username,password);
		HttpSession session= req.getSession();
		if(user!=null){
			//System.out.println("登陆成功");
			//session.setAttribute("user",user);
			/*session.setAttribute("usertype",user.getUser_type());*/
			String usertype=userService.getUserType(username);
			System.out.println("usertype:" + usertype);
			session.setAttribute("usertype",usertype);

			session.setAttribute("username",user.getUsername());
			session.setAttribute("id",user.getId());
			resp.sendRedirect(req.getContextPath()+"/index.jsp");
		}else {
			//System.out.println("登陆失败");
			session.setAttribute("usertype",null);
			req.getRequestDispatcher("user/login.jsp").forward(req,resp);
		}
	}
}
